﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using LibCommon;
using RTCCore;

namespace LibRTC.Core.MessageMap.SessionStateChangeEvent
{
    public class SessionPCToPCHandler : SimpleMessageHandler,ISessionChangeRegister
    {
        private static ILogger LOGGER = LoggerFactory.GetLogger("SessionPCToPCHandler", null);

        private RTCCoreClient _coreClient;

        private ISessionNew _sessionNew;

        private OnSessionConnectedDelegate _onSessionConnected;
        private OnSessionDisconnectedDelegate _onSessionDisconnected;

        public SessionPCToPCHandler()
        {
            _rtcEvent = RTC_EVENT.RTCE_SESSION_STATE_CHANGE;
            _name = "SessionPCToPCHandler";

        }

        public void SetRTCCoreClient(RTCCoreClient client)
        {
            Debug.Assert(null != client);
            _coreClient = client;
        }

        public void SetISessionNew(ISessionNew sessionNew)
        {
            Debug.Assert(null != sessionNew);
            _sessionNew = sessionNew;
        }

        public override void Process(RTC_EVENT rtcEventType, object rtcEvent)
        {
            Debug.Assert(RTC_EVENT.RTCE_SESSION_STATE_CHANGE == rtcEventType);
            Debug.Assert(null != rtcEvent);
            Debug.Assert(rtcEvent is IRTCSessionStateChangeEvent);

            IRTCSessionStateChangeEvent sessionStateChangeEvent = (IRTCSessionStateChangeEvent) rtcEvent;

            LOGGER.LogDebug("" + sessionStateChangeEvent.State);

            switch (sessionStateChangeEvent.State)
            {
                case RTC_SESSION_STATE.RTCSS_INCOMING:

                    switch (sessionStateChangeEvent.Session.Type)
                    {
                        case RTC_SESSION_TYPE.RTCST_PC_TO_PC:

                            IRTCSession videoSession = sessionStateChangeEvent.Session;
                            IRTCParticipant participant = null;

                            foreach (IRTCParticipant element in videoSession.Participants)
                            {
                                participant = element;
                            }

                            Debug.Assert(null != participant);
                            Debug.Assert(null != _sessionNew);

                            _coreClient.RTCClient.PlayRing(RTC_RING_TYPE.RTCRT_PHONE, true);

                            _sessionNew.OnVideoSessionNew(participant.Name,participant.UserURI,videoSession);

                            break;

                        default:
                            break;
                    }

                    break;

                case RTC_SESSION_STATE.RTCSS_DISCONNECTED:

                    switch (sessionStateChangeEvent.Session.Type)
                    {
                        case RTC_SESSION_TYPE.RTCST_PC_TO_PC:
                            IRTCSession videoSession = sessionStateChangeEvent.Session;
                            Debug.Assert(null != videoSession);

                            if (null != _onSessionDisconnected)
                            {
                                _onSessionDisconnected(videoSession);
                            }

                            break;

                        default:
                            break;
                    }

                    break;

                case RTC_SESSION_STATE.RTCSS_CONNECTED:

                    switch (sessionStateChangeEvent.Session.Type)
                    {
                        case RTC_SESSION_TYPE.RTCST_PC_TO_PC:
                            IRTCSession videoSession = sessionStateChangeEvent.Session;
                            Debug.Assert(null != videoSession);

                            if (null != _onSessionConnected)
                            {
                                _onSessionConnected(videoSession);
                            }

                            break;

                        default:
                            break;
                    }

                    break;

                default:
                    break;
            }
        }

        public void Register(ISessionChange change)
        {
            Debug.Assert(null != change);
            _onSessionConnected += new OnSessionConnectedDelegate(change.OnSessionConnected);
            _onSessionDisconnected += new OnSessionDisconnectedDelegate(change.OnSessionDisconnected);
        }

        public void UnRegister(ISessionChange change)
        {
            Debug.Assert(null != change);
            _onSessionConnected -= new OnSessionConnectedDelegate(change.OnSessionConnected);
            _onSessionDisconnected -= new OnSessionDisconnectedDelegate(change.OnSessionDisconnected);
        }
    }
}
